***for each client, what was the method ranking in each group 
use "${PilotPublicData}/hgopy_pilot_counselling_public.dta", clear 
*extract coefs, loop over ranking position p
* extract coefs, loop over ranking position p
  forvalues p=1/5 {
    * calculate prop 
      prop mr`p' if view!=0, 
    * over view v
    * over method m
    forvalues m=1/5 {
      capture local p`p'_m`m' = _b[`m'.mr`p'] 
      if _rc==111 {
          local p`p'_m`m' = 0
      }
    }
  }

* now make a nice matrix with all this data 
  capture mat drop mr
  forvalues m=1/5 {
	    capture matrix drop mr`m'
	    forvalues p=1/5 {
		    matrix mr`m' = nullmat( mr`m' ), `p`p'_m`m''
 		}
		matrix mr = nullmat(mr) \ mr`m'
	}
  mat list mr
  
* Output to tex
  clear
  svmat mr

*create a var for varnames 
  gen v1=""
  order v1, first 
  
* row titles 
  local methods = "IUD Implant Pill Injectable LAM"
  tokenize `methods'
  forvalues i=1/5 {
      replace v1="``i''" if _n==`i'
  }
  
* format
  ds v1, not 
  foreach v in `r(varlist)' {
    tostring `v', format(%9.2f) replace force
    replace `v'="" if `v'=="."
  }

* Total
  gen nt="" 
  replace nt = "1.00" if inlist(v1,"IUD","Implant","Pill","Injectable","LAM")

  local N = _N + 1
  set obs `N'
  gen n=_n
  forvalues i=1/5 {
      replace mr`i'="1.00" if _n>(`N'-1)
  }
  replace v1="Total" if _n>(`N'-1)
  replace n=6.5 if n==(`N'-1)
  replace n=12.5 if n==(`N')
  sort n
  drop n
  drop nt
  
* Export  
  #delimit ;
  texsave_custom using "${OutTexTab}/TabS6-rankings.tex" , replace nonames frag nofix
    align("lcccccc")
    headerlines(" & (1) & (2) & (3) & (4) & (5) "
          " & Pos. 1 & Pos. 2 & Pos. 3 & Pos. 4 & Pos. 5 ")	; 
  #delimit cr

